import { Injectable } from '@nestjs/common'
import { User } from '../entities/user.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { v4 as uuidv4 } from 'uuid'

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>
  ) {}

  async save(data: Partial<User>): Promise<any> {
    return await this.userRepository.save(data)
  }

  async find(option?: Object): Promise<any> {
    return await this.userRepository.find(option)
  }

  async findOne(option?: Object, getPassword: Boolean = false): Promise<any> {
    const res = await this.userRepository.findOne(option)
    if (res && getPassword) delete res.password
    return res
  }

  async updateToken(option: any): Promise<any> {
    return await this.userRepository.update(option, { token: uuidv4() })
  }
}
